package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.management.RuntimeErrorException;

import com.neuedu.oasystem.dao.IParticipantDao;
import com.neuedu.oasystem.entity.Participant;
import com.neuedu.oasystem.util.DBHelper;


public class ParticipantDaoImpl extends BaseDao implements IParticipantDao {
	

	@Override
	public boolean add(Participant param) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();

		try {
			String sql = "insert into PATICIPAT(Emp_Id,Meeting_Id,conference_Theme,present_Situation) values (?,?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, param.getEmpId());
			statement.setInt(2, param.getMeetingId());
			statement.setString(3, param.getConferenceTheme());
			statement.setString(4, param.getPresentSituation());
			int count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}

		return false;
	}


	@Override
	public boolean edit(Participant param) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "update PATICIPAT set Meeting_Id=?,conference_Theme=?,present_Situation=? where Emp_Id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(4, param.getEmpId());
			statement.setString(2, param.getConferenceTheme());
			statement.setString(3, param.getPresentSituation());
			statement.setInt(1, param.getMeetingId());

			int count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}
		return false;
	}


	@Override
	public boolean deleteById(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "delete from PATICIPAT where Emp_Id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			int count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}
		return false;
	}

	@Override
	public Participant findById(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Participant participant = null;
		connection = getConnection();
		try {
			String sql = "select Emp_Id,Meeting_Id,conference_Theme,present_Situation from PATICIPAT where Emp_Id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			if (resultSet.next()) {
				int id1 = resultSet.getInt(1);
				int meetingId = resultSet.getInt("Meeting_Id");
				String conferenceTheme = resultSet.getString("Conference_Theme");
				String presentSituation = resultSet.getString("present_Situation");
				participant = new Participant(id1, meetingId, conferenceTheme, presentSituation);
			}
		} catch (SQLException e) {
			//e.printStackTrace();
			//throw new RuntimeException(e.getMessage());
		}
		return participant;
	}


	@Override
	public List<Participant> findAll() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Participant participant;
		List<Participant> list = new ArrayList<Participant>();

		connection = getConnection();
		try {
			String sql = "select Emp_Id,MEETING_ID,CONFERENCE_THEME,PRESENT_SITUATION from PATICIPAT";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int empId = resultSet.getInt("Emp_Id");
				int meetingId = resultSet.getInt("Meeting_Id");
				String conferenceTheme = resultSet.getString("Conference_Theme");
				String presentSituation = resultSet.getString("present_Situation");
				participant = new Participant(empId, meetingId, conferenceTheme, presentSituation);
				list.add(participant);
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(resultSet, statement, null);
		}

		return list;
	}
	

	@Override
	public List<Participant> findByExample(Participant param) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Participant> list = new ArrayList<Participant>();
		Participant participant;
		connection = getConnection();
		try {
			
			String sql = "select Emp_Id,Meeting_Id,conference_Theme,present_Situation from PATICIPAT where Meeting_Id =? ";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, param.getMeetingId());
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int meetingId = resultSet.getInt(1);
				int empId = resultSet.getInt("Meeting_Id");
				String conferenceTheme = resultSet.getString("conference_Theme");
				String presentSituation = resultSet.getString("present_Situation");
				participant = new Participant( empId,meetingId, conferenceTheme, presentSituation);
				list.add(participant);
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(resultSet, statement, null);
		}

		return list;
	}

	@Override
	public List<Participant> findByPager(int page, int size) {
		
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		
		return 0;
	}
	

	@Override
	public int addAll(List<Participant> useparamrs) {
		int result = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "insert into PATICIPAT(Emp_Id,Meeting_Id,CONFERENCE_THEME,PRESENT_SITUATION) "
					+ "values (?,?,?,?)";
			statement = connection.prepareStatement(sql);
			for (Participant participant : useparamrs) {
				statement.setInt(1, participant.getEmpId());
				statement.setInt(2, participant.getMeetingId());
				statement.setString(3, participant.getConferenceTheme());
				statement.setString(4, participant.getPresentSituation());
				statement.addBatch();
			}
			int[] count = statement.executeBatch();
			
			for (int i : count) {
				if (i==-2) {
					result++;
				}
			}
			if (result==count.length) {
				return result;
			}
			System.out.println(result);
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		}finally {
			DBHelper.closeAll(null, statement, null);
		}
 		return result;
	}

	@Override
	public int editAll(List<Participant> param) {
		// TODO Auto-generated method stub
		return 0;
	}


}
